package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 非递减子序列
 * @author qulq
 * @date 2024-07-07 22:11:58
 */
public class T491_NonDecreasingSubsequences{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T491_NonDecreasingSubsequences().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<Integer>> ans = new ArrayList<>();
	List<Integer> path = new ArrayList<>();

    public List<List<Integer>> findSubsequences(int[] nums) {
		backtracking(nums, 0);
		return ans;
    }

	private void backtracking(int[] nums, int start) {
		if (path.size() >= 2) {
			ans.add(new ArrayList<>(path));
		}

		// 区别于used数组，用set来记录同一数层的元素是否有使用过，如[4,7,6,7]，第二个7的分支就不能选取了
		// set在函数内定义，每层递归都是一个新的set
		Set<Integer> set = new HashSet<>();
		for (int i = start; i < nums.length; i++) {
			if (set.contains(nums[i]) || (path.size() > 0 && nums[i] < path.get(path.size() - 1))) {
				continue;
			}
			set.add(nums[i]);
			path.add(nums[i]);
			backtracking(nums, i + 1);
			path.remove(path.size() - 1);
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}